package pers.supeijin.devtools.converter.wx2vue.converter.script;

import com.intellij.lang.javascript.psi.JSReferenceExpression;
import com.intellij.lang.javascript.psi.JSVariable;
import com.intellij.lang.javascript.psi.impl.JSChangeUtil;
import com.intellij.openapi.util.text.StringUtil;
import com.intellij.psi.PsiElement;
import com.intellij.psi.util.PsiTreeUtil;

/**
 * 事件相关转换辅助工具
 *
 * @author bluefox
 * @date 2021/2/1.
 */

public class EventConvertUtils {
	/**
	 * 事件数据集标准字段
	 */
	public static final String EVENT_DATASEA_TAG = ".currentTarget.dataset.";
	public static final String WX_TRIGGRT_EVENT_TAG = "this.triggerEvent";
	public static final String VUE_TRIGGRT_EVENT_TAG = "this.$emit";

	public EventConvertUtils() {
	}

	public static void convertEvent() {

	}


	public static void convertFunction() {

	}

	/**
	 * 转换数据集合
	 *
	 * @param psiElement
	 */
	public static void convertDataset(PsiElement psiElement) {
		PsiElement[] psiElements = PsiTreeUtil.collectElements(psiElement, element -> element instanceof JSReferenceExpression
				&& !(element.getParent() instanceof JSReferenceExpression) && element.getText().indexOf(EVENT_DATASEA_TAG) > 0);
		for (PsiElement element : psiElements) {
			JSReferenceExpression jsReferenceExpression = (JSReferenceExpression) element;
			PsiElement parent = jsReferenceExpression.getParent();
			String jsReferenceExpressionText = jsReferenceExpression.getText();
			int indexOf = jsReferenceExpressionText.indexOf(EVENT_DATASEA_TAG);
			int keyLength = EVENT_DATASEA_TAG.length();
			String substring = jsReferenceExpressionText.substring(indexOf + keyLength);
			if (parent instanceof JSVariable) {
				//key = e.currentTarget.dataset.key;
				String name = parent.getFirstChild().getText();
				if (StringUtil.equals(name, substring) || StringUtil.equals(name, substring + ";")) {
					parent.getParent().delete();//删除当前行代码
				} else {
					EventConvertUtils.replaceJSReferenceExpression(parent, jsReferenceExpression, substring);
				}
			} else {
				EventConvertUtils.replaceJSReferenceExpression(parent, jsReferenceExpression, substring);
			}
		}
	}

	/**
	 * 替换发送事件 相关代码实现
	 * this.triggerEvent ==> this.$emit
	 *
	 * @param psiElement
	 */
	public static void converttriggerEvent(PsiElement psiElement) {
		PsiElement[] collectElements = PsiTreeUtil.collectElements(psiElement, element -> element instanceof JSReferenceExpression && element.getText().startsWith(WX_TRIGGRT_EVENT_TAG));
		for (PsiElement element : collectElements) {
			JSReferenceExpression jsReferenceExpression = (JSReferenceExpression) element;
			PsiElement parent = PsiTreeUtil.getStubOrPsiParent(jsReferenceExpression);
			String jsReferenceExpressionText = jsReferenceExpression.getText();
			jsReferenceExpressionText = jsReferenceExpressionText.replace(WX_TRIGGRT_EVENT_TAG, VUE_TRIGGRT_EVENT_TAG);
			EventConvertUtils.replaceJSReferenceExpression(parent, jsReferenceExpression, jsReferenceExpressionText);
		}
	}

	/**
	 * 替换表js达式
	 * @param parent                       表达式的父级元素
	 * @param jsReferenceExpression        当前的表达式对象
	 * @param newJSReferenceExpressionText 新的表达式文本
	 */
	private static void replaceJSReferenceExpression(PsiElement parent, JSReferenceExpression jsReferenceExpression, String newJSReferenceExpressionText) {
		if (parent != null && jsReferenceExpression != null) {
			final JSReferenceExpression expressionPsiWithContext = JSChangeUtil.createExpressionPsiWithContext(newJSReferenceExpressionText, jsReferenceExpression, JSReferenceExpression.class);
			parent.addBefore(expressionPsiWithContext, jsReferenceExpression);
			jsReferenceExpression.delete();
		}
	}

}
